package cn.kai.util;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Objects;

/**
 * BigDecimal 工具类
 * Author: cKnight
 * Date: 2024/8/30
 */
@Slf4j
public class BDUtils {

    public static BigDecimal instance(Double d){
        return instance(String.valueOf(d));
    }

    public static BigDecimal instance(String s){
        return new BigDecimal(s);
    }

    public static BigDecimal instance(Integer i){
        return instance(String.valueOf(i));
    }

    public static BigDecimal mul(BigDecimal b1,BigDecimal b2){
        return b1.multiply(b2);
    }

    public static BigDecimal mul(BigDecimal b1,Integer i){
        return mul(b1,instance(i));
    }

    public static BigDecimal mul(BigDecimal b1,Double d){
        return mul(b1,instance(d));
    }

    public static BigDecimal mul(BigDecimal b1,String s){
        return mul(b1,instance(s));
    }

    public static BigDecimal scale(BigDecimal num,int scale, RoundingMode roundingMode){
        return num.setScale(scale,roundingMode);
    }

    public static BigDecimal scale(Double num,int scale, RoundingMode roundingMode){
        return scale(instance(num),scale,roundingMode);
    }

    public static String toStr(BigDecimal b1){
        return b1.toPlainString();
    }

    public static BigDecimal divide(BigDecimal b1,BigDecimal b2,int scale, RoundingMode roundingMode){
        if (Objects.isNull(b2) || instance("0").compareTo(b2) == 0){
            return BigDecimal.ZERO;
        }else {
            return b1.divide(b2,scale,roundingMode);
        }
    }

    public static BigDecimal divide(BigDecimal b1,Object b2,int scale,RoundingMode roundingMode){
        BigDecimal result = BigDecimal.ZERO;
        if (Objects.isNull(b2) || Objects.isNull(b1)){
            return result;
        }
        if (b2 instanceof Integer){
            Integer num = (Integer)b2;
            if (num == 0){
                return BigDecimal.ZERO;
            }else {
                result = divide(b1,instance(num),scale,roundingMode);
            }
        }else if (b2 instanceof Double){
            Double num = (Double)b2;
            if (num == 0){
                return BigDecimal.ZERO;
            }else {
                result = divide(b1,instance(num),scale,roundingMode);
            }
        }else if (b2 instanceof String){
            String num = (String)b2;
            if ("0".equals(num)){
                return BigDecimal.ZERO;
            }else {
                result = divide(b1,instance(num),scale,roundingMode);
            }
        }else if (b2 instanceof BigDecimal){
            BigDecimal num = (BigDecimal) b2;
            if (instance("0").compareTo(num) == 0){
                return BigDecimal.ZERO;
            }else {
                result = divide(b1,num,scale,roundingMode);
            }
        }
        return result;
    }

    public static BigDecimal divide(BigDecimal b1,Object b2){
        return divide(b1,b2,4,RoundingMode.HALF_UP);
    }

    /** 百分比 */
    public static Double percent(Integer i1 , Integer i2){
        return mul(divide(instance(i1),i2),100D).doubleValue();
    }

    public static Double percent(Double d1 , Double d2){
        return mul(divide(instance(d1),d2),100D).doubleValue();
    }

    /** 累加 */
    public static BigDecimal listAdd(Collection<BigDecimal> list){
        return list.stream().reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
    }
}
